package com.fmdj.mis.api.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.fmdj.common.exception.GlobalException;
import com.fmdj.common.util.CommonResult;
import com.fmdj.common.util.PageUtils;
import com.fmdj.mis.api.controller.form.SearchOrderByPageForm;
import com.fmdj.mis.api.controller.form.SearchOrderComprehensiveInfoForm;
import com.fmdj.mis.api.controller.form.SearchOrderLastGpsForm;
import com.fmdj.mis.api.service.OrderService;
import com.mchange.v1.io.WriterUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.javassist.bytecode.StackMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.BufferedOutputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/order")
@Tag(name = "OrderController", description = "订单模块接口")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/searchOrderByPage")
    @Operation(summary = "查询订单分页记录")
    @SaCheckPermission(value = {"ROOT", "ORDER:SELECT"}, mode = SaMode.OR)
    public CommonResult searchOrderByPage(@Valid @RequestBody SearchOrderByPageForm form) {
        PageUtils pageUtils = orderService.searchOrderByPage(form);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, pageUtils);
    }

    @PostMapping("/searchOrderComprehensiveInfo")
    @Operation(summary = "查询订单综合信息")
    @SaCheckPermission(value = {"ROOT", "ORDER:SELECT"}, mode = SaMode.OR)
    public CommonResult searchOrderComprehensiveInfo(@Valid @RequestBody SearchOrderComprehensiveInfoForm form) {
        HashMap results = orderService.searchOrderComprehensiveInfo(form.getOrderId());
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, results);
    }

    @PostMapping("/searchOrderLastGps")
    @Operation(summary = "查询某个订单最后一条(最新)Gps定位")
    @SaCheckPermission(value = {"ROOT", "ORDER:SELECT"}, mode = SaMode.OR)
    public CommonResult searchOrderLastGps(@Valid @RequestBody SearchOrderLastGpsForm form) {
        HashMap results = orderService.searchOrderLastGps(form);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, results);
    }


    //下载30天内的代驾上车点坐标记录(最新)Gps定位 最后是excel格式
    @PostMapping("/downLoadOrderStartLocationIn30Days")
    @Operation(summary = "下载30天内的代驾上车点坐标记录(最新)Gps定位")
    @SaCheckPermission(value = {"ROOT", "ORDER:SELECT"}, mode = SaMode.OR)
    public void downLoadOrderStartLocationIn30Days(HttpServletResponse response) {

        ArrayList<HashMap> result = orderService.searchOrderStartLocationIn30Days();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition","attachment;filename=heat_data.xls");
        try (
                //获取输出流
                ServletOutputStream out = response.getOutputStream();
                //将输出流包装成高效流
                BufferedOutputStream bos = new BufferedOutputStream(out)
        ) {
            ExcelWriter writer = ExcelUtil.getWriter();
            writer.write(result, true);//写入数据并且从头开始
            writer.flush(bos); //将数据刷到输出流中
            writer.close();
        } catch (Exception e) {
            throw new GlobalException("下载文件时出错");
        }
    }
}
